<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:09:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceAmountoddRepository;

/**
 * @property FinanceAmountoddRepository $repository 
 */
class AmountoddService extends Service
    {
    public $state = 1;
    public function boot(FinanceAmountoddRepository $repository)
        {
        $this->repository = $repository;
        }


    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    protected function save()
        {
        $data = request()->toArray();
        $id   = is_numeric($data['id'] ?? "") ? $data['id'] : 0;
        if ($id > 0) {
            $gdata = $this->repository->whereId($id)->first();
            if ($gdata->status_id != 0) {
                fail("单据已{$gdata->status->name}！不能编辑！");
                }
            }
        return $this->repository->autosave($data);
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据已{$gdata->status->name}！不能审核！");
                }
            auditSave($gdata);
            return $gdata->finance();
            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 1) {
                fail("单据已{$gdata->status->name}！不能反审！");
                }
            $gdata->nofinance();
            return auditSave($gdata, false);
            });
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("单据已{$v->status->name}！禁止删除");
                }
            return $v->delete();
            });
        }
    }